在微信小游戏中使用tensorflow的face

您所在的位置:网站首页 tensorflow js 3d 在微信小游戏中使用tensorflow的face

在微信小游戏中使用tensorflow的face

#在微信小游戏中使用tensorflow的face| 来源: 网络整理| 查看: 265

这篇文章有讲如何在微信小程序中用tensorflow做人脸识别。

https://zhuanlan.zhihu.com/p/81636351

做人脸识别,先要检测到人脸,获取面部特征,然后去数据库比对。

先看如何做人脸检测,获取轮廓和面部特征。

https://www.npmjs.com/package/@tensorflow-models/face-landmarks-detection

这个包可以检测到人脸468个点。查看这个包的源码,发现要下载3个模型文件:

facemesh:https://tfhub.dev/mediapipe/tfjs-model/facemesh/1/default/1iris:https://tfhub.dev/mediapipe/tfjs-model/iris/1/default/2

blazeface:https://tfhub.dev/tensorflow/tfjs-model/blazeface/1/default/1

很不幸,大概https://tfhub.dev被国内封锁了或者我们被封锁了,下载不了。

找来找去,找到中国区的这个网站:https://hub.tensorflow.google.cn/s?deployment-format=tfjs,分别去下载下来:

facemesh:https://r2---sn-2x3elnes.gvt1.com/edgedl/tfhub-tfjs-modules/mediapipe/tfjs-model/facemesh/1/default/1.tar.gziris:https://r2---sn-2x3elnes.gvt1.com/edgedl/tfhub-tfjs-modules/mediapipe/tfjs-model/iris/1/default/2.tar.gzblazeface:https://r2---sn-2x3elnes.gvt1.com/edgedl/tfhub-tfjs-modules/tensorflow/tfjs-model/blazeface/1/default/1.tar.gz

因为微信小程序不能从本地加载文件,所以必须解压,找个服务器传上去。可以开通“云开发”-“存储”或“云开发”-“静态网站”,把文件传上去。

const tf_face=require('@tensorflow-models/face-landmarks-detection'); let canvas_camera,ctx_camera; async function main() { console.log('load....'); console.log(tf_face.SupportedPackages.mediapipeFacemesh); // Load the MediaPipe Facemesh package. const model = await tf_face.load( tf_face.SupportedPackages.mediapipeFacemesh, { //detectorModelUrl:'https://tfhub.dev/mediapipe/tfjs-model/facemesh/1/default/1', detectorModelUrl:'https://???.tcloudbaseapp.com/model/facemesh/model.json', //irisModelUrl:'https://tfhub.dev/mediapipe/tfjs-model/iris/1/default/2', irisModelUrl:'https://???.tcloudbaseapp.com/model/iris/model.json', //modelUrl:'https://tfhub.dev/tensorflow/tfjs-model/blazeface/1/default/1' modelUrl:'https://???.tcloudbaseapp.com/model/blazeface/model.json' } ); console.log('aa'); canvas_camera=wx.createCanvas(); canvas_camera.width=300; canvas_camera.height=400; ctx_camera=canvas_camera.getContext('2d'); ctx_camera.drawImage(wx.tmGlobal.imgs['wxh'], 0,0,canvas_camera.width,canvas_camera.height); // Pass in a video stream (or an image, canvas, or 3D tensor) to obtain an // array of detected faces from the MediaPipe graph. If passing in a video // stream, a single prediction per frame will be returned. const predictions = await model.estimateFaces({ input: canvas_camera//document.querySelector("video") }); console.log('bb');

pc上,检测一张小图片,用web-gl,大概1.7秒。

再改进一下,不要每次都从云端加载模型文件,保存在本地文件系统,下次从手机本地加载。

const tf_core = require('@tensorflow/tfjs-core'); const tf_plugin = require('./js/libs/@tensorflow/tfjs-plugin/index.js'); const tf_face=require('@tensorflow-models/face-landmarks-detection'); tf_plugin.configPlugin({ //backendName:'wechat-webgl', fetchFunc: fetchWechat.fetchFunc(), tf:tf_core, webgl:tf_webgl, canvas: tf_canvas },true); let fs=wx.getFileSystemManager(); //const c_modelUrl='https://tfhub.dev/mediapipe/tfjs-model/facemesh/1/default/1'; const c_modelUrl='https://???.tcloudbaseapp.com/model/facemesh/model.json'; //const c_irisModelUrl='https://tfhub.dev/mediapipe/tfjs-model/iris/1/default/2'; const c_irisModelUrl='https://???.tcloudbaseapp.com/model/iris/model.json'; //const c_detectorModelUrl:'https://tfhub.dev/tensorflow/tfjs-model/blazeface/1/default/1'; const c_detectorModelUrl='https://???.tcloudbaseapp.com/model/blazeface/model.json'; const fileStorageHandler_blazeface = tf_plugin.fileStorageIO('face_blazeface', fs); const fileStorageHandler_iris = tf_plugin.fileStorageIO('face_iris', fs); const fileStorageHandler_facemesh = tf_plugin.fileStorageIO('face_facemesh', fs); ...... let isFaceModelSaved=wx.getStorageSync('isFaceModelSaved'); let model=null; if (isFaceModelSaved!='yes'){ console.log('load model from server'); model = await tf_face.load( tf_face.SupportedPackages.mediapipeFacemesh, { detectorModelUrl:c_detectorModelUrl, irisModelUrl:c_irisModelUrl, modelUrl:c_modelUrl, shouldLoadIrisModel:true } ); console.log(model); model.pipeline.irisModel.save(fileStorageHandler_iris) model.pipeline.meshDetector.save(fileStorageHandler_facemesh); model.pipeline.boundingBoxDetector.blazeFaceModel.save(fileStorageHandler_blazeface); wx.setStorage({ key:'isFaceModelSaved', data:'yes' }); } else{ console.log('from model from locale'); model = await tf_face.load( tf_face.SupportedPackages.mediapipeFacemesh, { detectorModelUrl:fileStorageHandler_blazeface, irisModelUrl:fileStorageHandler_iris, modelUrl:fileStorageHandler_facemesh, shouldLoadIrisModel:true } ); console.log(model); }



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3